package ca.cbc.giscommon.entity;

import com.vividsolutions.jts.geom.Geometry;
import org.hibernate.annotations.Cascade;

import javax.persistence.*;
import java.io.Serializable;
import java.sql.Timestamp;

/**
 * Content is the table store 3rd party feed
 */
@Entity
@Table(name = "twitter_tweets")
public class Tweet extends BaseEntity implements Cloneable, Serializable {

    private static final long serialVersionUID = -89765375036999860L;
    private long id;
    private Source source;
    private TwitterUser twitterUser;
    private String text;
    private String statusID;
    private Timestamp createdAt;
//    private Geometry theGeom;
    private Zone zone;

    public Tweet() {

    }

    public Tweet(String text, String statusId, long createdAt, Geometry theGeom, Source source, TwitterUser twitterUser) {
        this.setText(text);
        this.setStatusID(statusId);
        this.setCreatedAt(new Timestamp(createdAt));
        this.setTheGeom(theGeom);
        this.setSource(source);
        this.setTwitterUser(twitterUser);
    }

    @Override
    public Tweet clone() throws CloneNotSupportedException {
        return (Tweet) super.clone();
    }

    public boolean equals(Object obj) {
        if (obj.getClass().equals(Tweet.class) && ((Tweet) obj).getId() == this.getId()) {
            return true;
        }
        return false;
    }

    public void setZone(Zone zone) {
        this.zone = zone;
    }

    @Transient
    public Zone getZone() {
        return zone;
    }

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    public long getId() {
        return id;
    }

    @Column(name = "createdat")
    public Timestamp getCreatedAt() {
        return createdAt;
    }

    @ManyToOne
    @JoinColumn(name = "source_id")
    public Source getSource() {
        return source;
    }

//    @Column(name = "the_geom")
//    @Type(type = "org.hibernatespatial.GeometryUserType")
//    public Geometry getTheGeom() {
//        return theGeom;
//    }

    public String getText() {
        return text;
    }

    @Column(name = "status_id")
    public String getStatusID() {
        return statusID;
    }

    public void setStatusID(String statusID) {
        this.statusID = statusID;
    }

    @ManyToOne(cascade = {CascadeType.PERSIST, CascadeType.REFRESH})
    @Cascade(org.hibernate.annotations.CascadeType.SAVE_UPDATE)
    @JoinColumn(name = "screen_name")
    public TwitterUser getTwitterUser() {
        return twitterUser;
    }

    public void setTwitterUser(TwitterUser twitterUser) {
        this.twitterUser = twitterUser;
    }

    public void setId(long id) {
        this.id = id;
    }

    public void setCreatedAt(Timestamp createdAt) {
        this.createdAt = createdAt;
    }

    public void setSource(Source source) {
        this.source = source;
    }

//    public void setTheGeom(Geometry theGeom) {
//        this.theGeom = theGeom;
//    }

    public void setText(String text) {
        this.text = text;
    }

    public String toString() {
        StringBuilder sf = new StringBuilder();
        sf.append("Tweet {id: ").append(id)
                .append(", text:").append(text)
                .append(", statusID:").append(statusID)
                .append(", createdAt:").append(createdAt)
                .append(", source:").append(source)
                .append(", twitterUser:").append(twitterUser)
                .append(", theGeom:").append(theGeom)
                .append("}");

        return sf.toString();
    }
}
